<template>
    <div class="repair-copy-search-layout">
        <yd-search v-model="searchContent" :on-submit="submitHandler" :on-cancel="cancelHandler"></yd-search>

        <div class="repair-copy-title-part">
            <span>常用</span>
        </div>

        <div class="weui-cells_checkbox check-cells-part" v-for="(item,index) in historyUsers">
            <label class="weui-cell weui-check__label bottom-line" >
                <img src="../../assets/logo.jpg" class="check-logo-part">
                <div class="weui-cell__bd check-name-part">{{item.name+'(****'+item.mobile.substr(7,11)+')'}}</div>
                <div class="weui-cell__hd">
                    <input :disabled="item.isCheck"
                           type="checkbox"
                           v-model="checkedUsers"
                           :value="item.uuid"
                           name="assistance"
                           class="weui-check">
                    <i class="weui-icon-checked"></i>
                </div>
            </label>
        </div>

        <div class="repair-copy-title-part">
            <span>人员列表</span>
        </div>

        <div class="weui-cells_checkbox check-cells-part" v-for="(item,index) in allUsers">
            <label class="weui-cell weui-check__label bottom-line">
                <img src="../../assets/logo.jpg" class="check-logo-part">
                <div class="weui-cell__bd check-name-part">{{item.name+'(****'+item.mobile.substr(7,11)+')'}}</div>
                <div class="weui-cell__hd">
                    <input :disabled="item.isCheck" type="checkbox" v-model="checkedUsers" :value="item.uuid" name="assistance" class="weui-check">
                    <i class="weui-icon-checked"></i>
                </div>
            </label>
        </div>
        <div style="height: 1.4rem"></div>

        <div class="check-button-layout buttom-tabbar">
            <a href="javascript:;" @click="handleClick" class="weui-btn weui-btn_primary">{{'已选'+checkedUsers.length+'人,确定'}}</a>
        </div>

    </div>
</template>

<script>
    import {findCCList,orderCC} from "../../pcof/post";
    import {userInfoData,repairOrderSavedInstance} from '../../globalVariables';
    export default {
        name: "repairCopySearch",
        data() {
            return {
                taskUUid: '',
                searchContent: '',
                allUsers: [],
                historyUsers: [],
                checkedUsers: [],
                repairData: [],
            }
        },
        created() {
            //接受上个页面传值 在实例创建完成后被立即调用
            this.taskUUid = this.$route.params.taskUUid;
            this.repairData = JSON.parse(localStorage.getItem(repairOrderSavedInstance));
            console.log(this.taskUUid);
            if (this.repairData[10].ccList.length > 0) {
                this.checkedUsers = this.repairData[10].ccList;
            }
            this.findCCListFunc();
        },
        watch: {
            searchContent: function (newValue,oldValue) {
                this.checkedUsers = [];
                if (newValue === null || newValue === '' || newValue.length <= 0) {
                    this.searchContent = '';
                    this.findCCListFunc();
                } else {
                    this.searchContent = newValue;
                    this.findCCListFunc();
                }
            }
        },
        methods: {
            handleClick() {
                if (this.taskUUid === null || this.taskUUid === '') {
                    var list=[];
                    for (var i = 0;i<this.checkedUsers.length;i++) {
                        for (var j=0;j<this.allUsers.length;j++) {
                            if (this.checkedUsers[i] === this.allUsers[j].uuid) {
                                list.push(this.allUsers[j].name+'(****'+this.allUsers[j].mobile.substr(7,11)+')')
                            }
                        }
                    }
                    this.repairData[10].ccList = this.checkedUsers;
                    this.repairData[10].content = list.toString();
                    localStorage.setItem(repairOrderSavedInstance,JSON.stringify(this.repairData));
                    this.$router.back();
                } else {
                    this.orderCCFunc();
                }
            },
            submitHandler(value) {
                this.searchContent = value;
                this.findCCListFunc();
            },
            cancelHandler() {
                this.searchContent = '';
                this.findCCListFunc();
            },
            async orderCCFunc() {
                this.$dialog.loading.open('加载中...');
                var response = await orderCC(
                    JSON.parse(localStorage.getItem(userInfoData)).mobile,
                    this.taskUUid,
                    this.checkedUsers.toString()
                );
                if (response.data.code === 200) {
                    this.$dialog.loading.close();
                    this.$router.back();
                } else {
                    this.$dialog.loading.close();
                    this.$dialog.toast({
                        mes: response.data.msg,
                        timeout: 1500
                    });
                }

            },
            async findCCListFunc() {
                this.$dialog.loading.open('加载中...');
                var response = await findCCList(
                    JSON.parse(localStorage.getItem(userInfoData)).mobile,
                    this.searchContent,
                    this.taskUUid,
                );

                if (response.data.code === 200) {
                    this.$dialog.loading.close();
                    console.log(this.checkedUsers);
                    var allUserList = response.data.data.allUsers;
                    var historyUserList = response.data.data.historyUsers;
                    var checkedUserList = this.checkedUsers;
                    if (this.taskUUid === null || this.taskUUid === '' || this.taskUUid === undefined) {

                    } else {
                        console.log('taskUuid is not null');
                        for (var i = 0;i<checkedUserList.length;i++) {

                            for (var j = 0;j<historyUserList.length;j++) {
                                if (checkedUserList[i] === historyUserList[j].uuid) {
                                    console.log(checkedUserList[i] === historyUserList[j].uuid);
                                    // historyUserList[j]['isCheck'] = new Boolean(true);
                                    historyUserList[j]['isCheck'] = true;
                                } else {
                                    // historyUserList[j]['isCheck'] = new Boolean(false);
                                    historyUserList[j]['isCheck'] = false;
                                }
                            }

                            for (var z = 0;z<allUserList.length;z++) {
                                if (checkedUserList[i] === allUserList[z].uuid) {
                                    // allUserList[z]['isCheck'] = new Boolean(true);
                                    allUserList[z]['isCheck'] = true;
                                } else {
                                    // allUserList[z]['isCheck'] = new Boolean(false);
                                    allUserList[z]['isCheck'] = false;
                                }
                            }
                        }
                    }

                    this.allUsers = allUserList;
                    this.historyUsers = historyUserList;
                } else {
                    this.$dialog.loading.close();
                    this.$dialog.toast({
                        mes: response.data.msg,
                        timeout: 1500
                    });
                }
            },
            changeHistoryHandler(value,isCheckAll) {
                console.log(value)
            },
            changeUsersHandler(value,isCheckAll) {
                console.log(value)
            },
        },
    }
</script>

<style lang="scss">
    .repair-copy-search-layout {

        .repair-copy-title-part {
            background: #e6e6e6;
            width: 100%;
            padding-top: 0.2rem;
            padding-bottom: 0.2rem;
        }

        .check-cells-part {

            .check-logo-part {
                width: 0.6rem;
                height: 0.6rem;
                border-radius: 0.5rem;
            }

            .check-name-part {
                text-align: left;
                padding-left: 0.2rem;
            }

        }

        .check-button-layout {
            display: flex;
            background: #fafafa;
            padding-bottom: 0.2rem;
            padding-top: 0.2rem;

            .check-button-part {
                margin: 0 auto;
                width: 80%;
            }

            .weui-btn {
                display: flex;
                margin-top: 0;
                height: 0.6rem;
                align-items: center;
                justify-content: center;
                font-size: 1.2em;

            }
        }




    }
</style>